﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;

/**
 * <Jason>
 * <Date: 20131114>
 * 
 */
namespace VietThuongLMS.Services
{
    public class NhanVienService
    {

        public List<NhanVien> GetNhanVienList()
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<NhanVien> list = context.NhanViens.Include("LoaiTaiKhoan").Where(m => m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_TEACHER).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::GetNhanVienList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static NhanVienViewModel GetNhanVienList(int pageNum, int pageIndex)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<NhanVien> list = context.NhanViens.Include("TaiKhoan")
                        .OrderBy(m => m.TaiKhoan.NgayDangKy).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                    foreach (NhanVien nv in list)
                    {
                        nv.TaiKhoan.LoaiTaiKhoanReference.Load();
                        nv.ChiNhanhReference.Load();
                    }
                    int total = context.NhanViens.Count();
                    return new NhanVienViewModel(list, total);
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::GetNhanVienList", ex.StackTrace, ex.Message);
                return new NhanVienViewModel();
            }
        }

        public static bool InsertNhanVien(NhanVien comp)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    context.AddToNhanViens(comp);
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::InsertNhanVien", ex.StackTrace, ex.Message);
                return false;
            }
        }

        public static bool DeleteNhanVien(int maNhanVien)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    NhanVien obj = context.NhanViens.Where(c => c.ID == maNhanVien).FirstOrDefault();
                    if (obj != null)
                    {
                        context.NhanViens.DeleteObject(obj);
                        context.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::DeleteNhanVien", ex.StackTrace, ex.Message);
                return false;
            }
        }

        public static NhanVien GetNhanVienById(int id)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    NhanVien nv = new NhanVien();
                    nv = context.NhanViens.Include("TaiKhoan").Where(c => c.ID == id).FirstOrDefault();
                    //NhanVien nv = new NhanVien();
                    //nv = context.NhanViens.Join(context.TaiKhoans,
                    //n => n.MaTaiKhoan,
                    //tk => tk.ID,
                    //(n, tk) => n).Join(context.ChiNhanhs,
                    //n => n.MaChiNhanh,
                    //c => c.ID,
                    //(n, c) => n).Where(n => n.ID == id).FirstOrDefault();

                    nv.ChiNhanhReference.Load();
                    return nv;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVientServices::GetNhanVienById", ex.StackTrace, ex.Message);
                return null;
            }
        }

        //public static ChiNhanh GetNhanVienChiNhanhById(int id)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            ChiNhanh cn = new ChiNhanh();

        //            //cn = context.ChiNhanhs.Where(c => c.NhanVien

        //            //cn = context.NhanViens.Join(context.TaiKhoans,
        //            //n => n.MaTaiKhoan,
        //            //tk => tk.ID,
        //            //(n, tk) => n).Join(context.ChiNhanhs,
        //            //n => n.MaChiNhanh,
        //            //c => c.ID,
        //            //(n, c) => n).Where(n => n.ID == id).FirstOrDefault();
        //            return cn;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("NhanVientServices::GetNhanVienById", ex.StackTrace, ex.Message);
        //        return null;
        //    }
        //}

        public static bool Edit(NhanVien newModel)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    NhanVien model = context.NhanViens.Where(c => c.ID == newModel.ID).FirstOrDefault();

                    if (model != null)
                    {
                        model.MaChiNhanh = newModel.MaChiNhanh;
                        context.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::Edit", ex.StackTrace, ex.Message);
                return false; 
            }
        }

        public static int GetStaffIdByAccountID(int account_id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                NhanVien nv = context.NhanViens.Where(c => c.MaTaiKhoan == account_id).FirstOrDefault();
                if (nv != null)
                    return nv.ID;
                return -1;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::GetStaffIdByAccountID", ex.StackTrace, ex.Message);
                return -1;
            }
            finally
            {
                context.Dispose();
            }
        }

        //Added by K
        public int GetChiNhanhNhanVien(string mtk)
        {
            int rs = -1;
            var context = new LMSDataEntities();
            try {
                rs = (int)(from nv in context.NhanViens
                      from tk in context.TaiKhoans
                      where tk.MaTaiKhoan == mtk && nv.MaTaiKhoan == tk.ID
                      select nv.MaChiNhanh).FirstOrDefault();
                return rs;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("NhanVienServices::GetChiNhanhNhanVien", ex.StackTrace, ex.Message);
                throw ex;
                return rs;
            }
            finally {
                context.Dispose();
            }
            
        }
        //End Addition
        

    }
}
